package com.teaching.teachingsupport.service.impl.teacherserviceimpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.teachingsupport.common.pojo.dto.response.HomeworkListResponse;
import com.teaching.teachingsupport.common.pojo.dto.response.SyllabusDTO;
import com.teaching.teachingsupport.common.pojo.dto.response.TodoDTO;
import com.teaching.teachingsupport.common.enums.HomeworkStatus;
import com.teaching.teachingsupport.service.service2.teacherservice.TeacherTodoService;
import com.teaching.teachingsupport.service.service2.teacherservice.TeacherHomeworkService;
import com.teaching.teachingsupport.service.service2.teacherservice.SyllabusService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TeacherTodoServiceImpl implements TeacherTodoService {

    private final TeacherHomeworkService teacherHomeworkService;
    private final SyllabusService syllabusService;

    @Override
    public List<TodoDTO> getTeacherTodoList(Long userId) {
        List<TodoDTO> todos = new ArrayList<>();

        // 1. 获取需要批改的作业
        Page<HomeworkListResponse> homeworkPage = teacherHomeworkService.getHomeworkList(
            HomeworkStatus.UNGRADED,  // 获取待批改的作业
            null,  // 不筛选课程名称
            1,     // 第一页
            100    // 最多100条
        );
        
        if (homeworkPage != null && homeworkPage.getRecords() != null) {
            List<TodoDTO> homeworkTodos = homeworkPage.getRecords().stream()
                .map(homework -> TodoDTO.builder()
                    .type("HOMEWORK")
                    .title("批改作业：" + homework.getTitle())
                    .description(String.format(
                        "课程：%s，班级：%s，已提交人数：%d/%d",
                        homework.getCourseName(),
                        homework.getClassName(),
                        homework.getSubmittedCount(),
                        homework.getTotalCount()
                    ))
                    .deadline(homework.getDeadline())
                    .status(0)
                    .createTime(homework.getCreateTime())
                    .sourceId(homework.getId())
                    .sourceName(homework.getCourseName())
                    .build())
                .collect(Collectors.toList());
            todos.addAll(homeworkTodos);
        }

        // 2. 获取需要审核的教学大纲
        List<SyllabusDTO> syllabusList = syllabusService.getSyllabusList("PENDING", null);
        if (syllabusList != null) {
            List<TodoDTO> syllabusTodos = syllabusList.stream()
                .map(syllabus -> TodoDTO.builder()
                    .type("SYLLABUS")
                    .title("审核教学大纲：" + syllabus.getCourseName())
                    .description(String.format(
                        "课程：%s，适用年级：%s",
                        syllabus.getCourseName(),
                        String.join("、", syllabus.getGrades())
                    ))
                    .deadline(syllabus.getCreateTime().plusDays(7))
                    .status(0)
                    .createTime(syllabus.getCreateTime())
                    .sourceId(syllabus.getId())
                    .sourceName(syllabus.getCourseName())
                    .build())
                .collect(Collectors.toList());
            todos.addAll(syllabusTodos);
        }

        return todos;
    }
} 